//
//  SHButton.m
//  YHSJ
//
//  Created by CYF on 16/11/10.
//  Copyright © 2016年 SH. All rights reserved.
//

#import "SHButton.h"
#import "CALayer+FSUI.h"
@interface SHButton ()



@end


@implementation SHButton

-(instancetype)initWithFrame:(CGRect)frame{
    if ( self = [super initWithFrame:frame]) {
      

    
    }
    return self;
}

#pragma mark - 1类方法创建按钮  设置button 样式 标题  字体 字体颜色16进制 居 左 中 右 状态
+ (instancetype)buttonWithType:(UIButtonType)buttonType title:(NSString *)title font:(CGFloat)font titleColorString:(NSString *)color textAlignment:(UIControlContentHorizontalAlignment)textAlignment forState:(UIControlState)state{
    
    SHButton *btn = [SHButton buttonWithType:buttonType title:title font:font titleColor:KSETCOLORWITHHEXSTRING(color) textAlignment:textAlignment forState:UIControlStateNormal];

    
    return btn;
    
    
}

#pragma mark - 2类方法创建按钮  设置button 样式 标题  字体 字体颜色对象 居 左 中 右 状态
+ (instancetype)buttonWithType:(UIButtonType)buttonType title:(NSString *)title font:(CGFloat)font titleColor:(UIColor *)color textAlignment:(UIControlContentHorizontalAlignment)textAlignment forState:(UIControlState)state{
    
    SHButton *btn = [SHButton buttonWithType:buttonType];
    [btn setTitle:title font:font titleColor:color  textAlignment:textAlignment forState:state];
    
    return btn;
    
    
}


#pragma mark -- 设置button 标题  字体 字体颜色16进制   居 左 中 右 状态
- (void)setTitle:(NSString *)title font:(CGFloat)font titleColorString:(NSString *)color textAlignment:(UIControlContentHorizontalAlignment)textAlignment forState:(UIControlState)state{
    [self setTitle:title font:font titleColor:KSETCOLORWITHHEXSTRING(color) textAlignment:textAlignment forState:state];
}

#pragma mark -- 设置button 标题  字体 字体颜色对象   居 左 中 右 状态
- (void)setTitle:(NSString *)title font:(CGFloat)font titleColor:(UIColor *)color textAlignment:(UIControlContentHorizontalAlignment)textAlignment forState:(UIControlState)state{
    [self setTitle:title forState:state];
    [self setTitleColor:color forState:state];
    [self.titleLabel setFont:KSHSystemFont(font)];
    [self setContentHorizontalAlignment:textAlignment];
    
}
#pragma mark -- 设置button 上下左右都圆角 高效
- (void)setCornerRadius:(CGFloat)cornerRadius bounds:(CGRect)bounds{
    
   
    [self setCornerRadius:cornerRadius bounds:bounds corner:UIRectCornerAllCorners];
    
}
#pragma mark -- 设置button 上下左右 圆角不一样 高效

- (void)setCornerRadius:(CGFloat)cornerRadius bounds:(CGRect)bounds corner:(UIRectCorner)corner{
    
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:bounds  byRoundingCorners:corner cornerRadii:CGSizeMake(__I6Width(cornerRadius),__I6Width(cornerRadius))];
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = bounds;
    maskLayer.path = maskPath.CGPath;
    self.layer.mask = maskLayer;
    
}


#pragma mark -- 设置button 圆角 效率低 - 废弃
- (void)setCornerRadius:(CGFloat)cornerRadius{
    self.layer.cornerRadius = __I6Width(cornerRadius);
    self.layer.masksToBounds = YES;
}
#pragma mark -- 设置button 边框宽度 边框颜色
- (void)setBorderWidth:(CGFloat)borderWidth borderColor:(UIColor *)borderColor{
    self.layer.borderWidth = borderWidth;
    self.layer.borderColor = borderColor.CGColor;
}






- (void)setHighlightedBackgroundColor:(UIColor *)highlightedBackgroundColor {
    _highlightedBackgroundColor = highlightedBackgroundColor;
    if (_highlightedBackgroundColor) {
        // 只要开启了highlightedBackgroundColor，就默认不需要alpha的高亮
        self.adjustsButtonWhenHighlighted = NO;
    }
    
    
}

- (void)setHighlighted:(BOOL)highlighted {
    [super setHighlighted:highlighted];
    
    
    // 渲染背景色
    if (self.highlightedBackgroundColor ) {
        [self adjustsButtonHighlighted];
    }
    // 如果此时是disabled，则disabled的样式优先
    if (!self.enabled) {
        return;
    }
    // 自定义highlighted样式
    if (self.adjustsButtonWhenHighlighted) {
        if (highlighted) {
            self.alpha = 0.5f;
        } else {
            [UIView animateWithDuration:0.25f animations:^{
                self.alpha = 1;
            }];
        }
    }
}

- (void)adjustsButtonHighlighted {
    if (self.highlightedBackgroundColor) {
        if (!self.highlightedBackgroundLayer) {
            self.highlightedBackgroundLayer = [CALayer layer];//创建一个新的不带动画的layer
            [self.highlightedBackgroundLayer FS_removeDefaultAnimations];//移除系统默认的隐性动画
            [self.layer insertSublayer:self.highlightedBackgroundLayer atIndex:0];//替换为自定义的layer
        }
        //为新的layer添加属性
        self.highlightedBackgroundLayer.frame = self.bounds;
        self.highlightedBackgroundLayer.cornerRadius = self.layer.cornerRadius;
        self.highlightedBackgroundLayer.backgroundColor = self.highlighted ? self.highlightedBackgroundColor.CGColor : [UIColor colorWithRed:1 green:1 blue:1 alpha:0].CGColor;
    
    }
    
    
    
    
}


@end
